﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;
using AjaxControlToolkit;

using System.Linq;
using SD.LLBLGen.Pro.ORMSupportClasses;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMS.Controls
{
    public partial class Menu : UserControlBase
    {
        private List<ActionEntity> CurrentListMenu
        {
            get
            {
                if (ViewState["CurrentListMenu"] == null)
                {
                    return new List<ActionEntity>();
                }
                return (List<ActionEntity>)ViewState["CurrentListMenu"];
            }
            set
            {
                ViewState["CurrentListMenu"] = value;
            }
        }
 
        public List<ActionEntity> GetPermissionedMenuItems()
        {
            var userRoleCollection = new UserRoleCollection();
            userRoleCollection.GetMulti(UserRoleFields.UserId == CurrentControlLoginUser.UserID);

            var roleList = from entity in userRoleCollection select entity.RoleId;
            var compareExpression = new FieldCompareRangePredicate(RoleActionFields.RoleId, false, roleList.ToArray());
            var expression = new PredicateExpression();
            expression.Add(ActionFields.IsActive==true);
            expression.AddWithAnd(compareExpression);

            var list = new List<ActionEntity>();
            var collection = new ActionCollection();
            collection.GetMulti(expression,0,new SortExpression{ActionFields.SortIndex|SortOperator.Ascending}, new RelationCollection { ActionEntity.Relations.RoleActionEntityUsingActionId });
            foreach (var item in collection)
            {
                list.Add(item);
            }
            return list;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MenuLoaded = true;
                CurrentListMenu = GetPermissionedMenuItems();
                var root = from entity in CurrentListMenu
                           where entity.ParentId == string.Empty
                           orderby entity.SortIndex ascending
                           select entity;
                rptMenu.DataSource = root;
                rptMenu.DataBind();
            }
        }
         protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
             if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

                var rptChildren = e.Item.FindControl("rptChildren") as Repeater;
                if (rptChildren == null) return;
                var module = e.Item.DataItem as ActionEntity;
                if (module == null) return;
                var child = from entity in CurrentListMenu
                            where entity.ParentId == module.ActionId
                            orderby entity.SortIndex ascending
                            select entity;
                rptChildren.DataSource = child;
                rptChildren.DataBind();

                var panelModuleTitle = e.Item.FindControl("panelModuleTitle") as Panel;
                if (panelModuleTitle != null)
                {
                    panelModuleTitle.Attributes["ModuleID"] = module.ActionId;
                }
                var cpeMenuItem = e.Item.FindControl("cpeMenuItem") as CollapsiblePanelExtender;
                //if (cpeMenuItem != null && CollapsedModules != null && CollapsedModules.Contains(module.ActionId))
                //{
                //    cpeMenuItem.Collapsed = true;
                //}
                var btnCollapse = e.Item.FindControl("btnCollapse") as ImageButton;
                if (cpeMenuItem != null && btnCollapse != null)
                {
                    //btnCollapse.ImageUrl = cpeMenuItem.Collapsed
                    //                           ? "~/Images/Leftmenu_sword_up.GIF"
                    //                           : "~/Images/Leftmenu_sword_down.GIF";
                    cpeMenuItem.ImageControlID = btnCollapse.UniqueID;
                }
            }
        }
        //protected override void OnPreRender(EventArgs e)
        //{
        //    //var listModuleExtenders = new Dictionary<string, string>();
        //    //foreach (RepeaterItem item in rptMenu.Items)
        //    //{
        //    //    var cpeMenuItem = item.FindControl("cpeMenuItem") as CollapsiblePanelExtender;
        //    //    var panelModuleTitle = item.FindControl("panelModuleTitle") as Panel;
        //    //    if (cpeMenuItem != null && panelModuleTitle != null)
        //    //    {
        //    //        listModuleExtenders.Add(cpeMenuItem.BehaviorID, panelModuleTitle.ClientID);
        //    //    }
        //    //}
        //    //ScriptStartup("var moduleExtenders=" + new JavaScriptSerializer().Serialize(listModuleExtenders) + ";");
        //    base.OnPreRender(e);
        //}
    }
}